//
//  HuLaQuanMapController.m
//  HuLaQuan
//
//  Created by liyan on 16/1/20.
//  Copyright © 2016年 yuwubao. All rights reserved.
//

#import "HuLaQuanMapController.h"
//导入头文件
#import <BaiduMapAPI/BMapKit.h>

#import "CustomOverlayView.h"
#import "CustomOverlay.h"

#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>

#import "SHBAVPlayerView.h"
@interface HuLaQuanMapController ()<BMKMapViewDelegate,BMKLocationServiceDelegate,UIAlertViewDelegate>
{
    //用来接收视频的地址信息的
    NSString *_urlString;
}
//player
@property(nonatomic,strong)SHBAVPlayerView *avView;

@property(strong,nonatomic)BMKMapView *mapView;
@property(strong,nonatomic)BMKLocationService *locService;

//getLocation
@property(assign,nonatomic)CGFloat latitude;
@property(assign,nonatomic)CGFloat longitude;

@end

@implementation HuLaQuanMapController
-(void)setupUI
{
    self.title = @"路况";
    
    [self setupNavBackButton];
    
    [self createMap];
    
    [self createAnnotation];
    
    [self createLine];
   // ([CLLocationManager locationServicesEnabled]判断用户是否允许定位的
    //初始化BMKLocationService
//    _locService = [[BMKLocationService alloc]init];
//    _locService.delegate =self;
//    //启动LocationService,进入地图开始显示自己的坐标
////    [_locService startUserLocationService];
//    _mapView.showsUserLocation = YES;
//    _mapView.userTrackingMode = BMKUserTrackingModeNone;
    

}
#pragma mark- UIAlertViewDelagate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (1 == buttonIndex) {
         NSString *path = [[NSBundle mainBundle] pathForResource:@"La La La" ofType:@"mp3"];
        
     
        
        if (!_avView) {
            _avView = [[SHBAVPlayerView alloc] initWithLocalMediaPath:path];
        }
        
        [self.avView play];
        
    }
    else if (2 == buttonIndex)
    {
        _urlString = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"mp4"];
        NSURL *url = nil;
        url = [NSURL fileURLWithPath:_urlString];
        
        AVPlayerViewController *player = [[AVPlayerViewController alloc]init];
        
        player.player = [[AVPlayer alloc]initWithURL:url];
        
        [self presentViewController:player animated:YES completion:nil];
        
//        if (!_avView) {
//            _avView = [[SHBAVPlayerView alloc] initWithLocalMediaPath:_urlString];
//        }
//        
//        [self.avView play];
    }
    else
    {
        [self.avView pause];
    }
    
}
//处理位置坐标更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    _mapView.centerCoordinate = userLocation.location.coordinate;//这个方法是让自己的位置显示在中心点上
    NSLog(@"当前位置%f,%f", userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude);
    
    self.latitude = userLocation.location.coordinate.latitude;
    self.longitude = userLocation.location.coordinate.longitude;
    
}

// 代理方法
#pragma mark - BMKLocationServiceDelegate
//实现相关delegate处理位置信息更新
//处理方向变更信息
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
    NSLog(@"heading is %@",userLocation.heading);
}


#pragma mark - 创建地图
- (void)createMap {
    //实例化一个地图
    _mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-64)];
    
    //设置地图经纬度的范围
    _mapView.region = BMKCoordinateRegionMake(CLLocationCoordinate2DMake(30.35, 114.17), BMKCoordinateSpanMake(0.01, 0.01));
    
    //设置地图的类型
    _mapView.mapType = BMKMapTypeStandard;//标准地图
    [self.view addSubview:_mapView];
    
//    [self.roadMap setImage:[UIImage imageNamed:@"lukuang"]forState:UIControlStateNormal];
//    [self.roadMap setTitle:@"路况" forState:UIControlStateNormal];
//    self.roadMap.imageEdgeInsets = UIEdgeInsetsMake(10, 9, 26, 26);
//    self.roadMap.titleEdgeInsets = UIEdgeInsetsMake(self.roadMap.imageEdgeInsets.right+3, 9, 26, 26);
    [self.view addSubview:_roadMap];

    [self.view addSubview:_homeMap];

    [self.view addSubview:_workMap];
}

#pragma mark - 大头针
- (void)createAnnotation {
    //实例化一个大头针数据
    BMKPointAnnotation *annotation = [[BMKPointAnnotation alloc] init];
    //设置经纬度
    annotation.coordinate = CLLocationCoordinate2DMake(30.35, 114.17);
    //设置主副标题
    annotation.title = @"湖北省";
    annotation.subtitle = @"武汉";
    
    //实例化一个大头针数据
    BMKPointAnnotation *pol = [[BMKPointAnnotation alloc] init];
    //设置经纬度
    pol.coordinate = CLLocationCoordinate2DMake(30.353, 114.17);
    //设置主副标题
    pol.title = @"湖北省";
    pol.subtitle = @"咸宁";
    
    //增加到地图-->会回调显示大头针视图的函数
    [_mapView addAnnotation:annotation];
    [_mapView addAnnotation:pol];
}

#pragma mark - 显示大头针的时候回调
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation {
    //判断标注的类型
    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
        NSString *annotationId = @"BMKPointAnnotation";
        //从复用队列获取
        BMKPinAnnotationView *annotationView = (BMKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:annotationId];
        if (annotationView == nil) {
            //复用队列没有那么创建新的
            annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationId];
        }
        //设置颜色
        annotationView.pinColor = BMKPinAnnotationColorRed;
        //设置 是否有下落效果
        annotationView.animatesDrop = YES;
        //是否显示气泡
        annotationView.canShowCallout = YES;
        //设置大头针数据 (大头针可复用)
        annotationView.annotation = annotation;
        
        return annotationView;
    }
    
    return nil;
}
-(void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:@"取消播放" otherButtonTitles:@"播放音频",@"播放视频", nil];
    [alert show];
    [self.mapView deselectAnnotation:view.annotation animated:YES];
}
#pragma mark - made roadLine
-(void)createLine
{
    CLLocationCoordinate2D coor1;
    coor1.latitude = 30.35;
    coor1.longitude = 114.17;
    BMKMapPoint pt1 = BMKMapPointForCoordinate(coor1);
    CLLocationCoordinate2D coor2;
    coor2.latitude = 30.353;
    coor2.longitude = 114.17;
    BMKMapPoint pt2 = BMKMapPointForCoordinate(coor2);
    BMKMapPoint * temppoints = new BMKMapPoint[2];
    temppoints[0].x = pt2.x;
    temppoints[0].y = pt2.y;
    temppoints[1].x = pt1.x;
    temppoints[1].y = pt1.y;
    CustomOverlay* custom = [[CustomOverlay alloc] initWithPoints:temppoints count:2];
    [_mapView addOverlay:custom];

}
- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay
{
    if ([overlay isKindOfClass:[CustomOverlay class]])
    {
        CustomOverlayView* cutomView = [[CustomOverlayView alloc] initWithOverlay:overlay];
        cutomView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:1];
        cutomView.fillColor = [[UIColor purpleColor] colorWithAlphaComponent:0.5];
        cutomView.lineWidth = 3.0;
        return cutomView;
        
        
    }
    return nil;
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [_mapView viewWillAppear];
    _mapView.delegate = self; // 此处记得不用的时候需要置nil，否则影响内存的释放
}
-(void)viewWillDisappear:(BOOL)animated
{
    [_mapView viewWillDisappear];
    _mapView.delegate = nil; // 不用时，置nil
    self.tabBarController.tabBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tabBarController.tabBar.hidden = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//touchClicked
- (IBAction)workMapShow:(id)sender {
}
- (IBAction)homeMapShow:(id)sender {
}
- (IBAction)roadMapShow:(id)sender {
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
